using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[ExecuteInEditMode]
	[AddComponentMenu("MaterialUI/Progress/Circular Progress Indicator")]
	public class CircularProgressIndicator : ProgressIndicator
	{
		[SerializeField]
		private RectTransform m_CircleRectTransform;

		private Image m_CircleImage;

		private VectorImage m_CircleIcon;

		private const float animDuration = 0.65f;

		private int m_AnimCircle;

		private float m_AnimCircleStartTime;

		private float m_AnimCircleCurrentFillAmount;

		private float m_AnimCircleCurrentRotation;

		private bool m_AnimColor;

		private float m_AnimColorStartTime;

		private Color m_AnimColorCurrentColor;

		private Color m_AnimColorTargetColor;

		private bool m_AnimSize;

		private float m_AnimSizeStartTime;

		private float m_AnimSizeCurrentSize;

		private float m_AnimSizeTargetSize;

		public RectTransform circleRectTransform
		{
			get
			{
				return m_CircleRectTransform;
			}
			set
			{
				m_CircleRectTransform = value;
			}
		}

		public Image circleImage
		{
			get
			{
				if (m_CircleImage == null && m_CircleRectTransform != null)
				{
					m_CircleImage = m_CircleRectTransform.GetComponent<Image>();
				}
				return m_CircleImage;
			}
		}

		public VectorImage circleIcon
		{
			get
			{
				if (m_CircleIcon == null && circleImage != null)
				{
					m_CircleIcon = circleImage.GetComponentInChildren<VectorImage>();
				}
				return m_CircleIcon;
			}
		}

		private void Start()
		{
			if (Application.isPlaying)
			{
				if (m_StartsHidden)
				{
					base.scaledRectTransform.localScale = new Vector3(0f, 0f, 1f);
				}
				else if (m_StartsIndeterminate)
				{
					StartIndeterminate();
				}
			}
		}

		private void Update()
		{
			UpdateAnimCircle();
			UpdateAnimColor();
			UpdateAnimSize();
		}

		private void UpdateAnimCircle()
		{
			if (circleImage == null || m_AnimCircle == 0)
			{
				return;
			}
			if (m_AnimCircle == 1)
			{
				float num = Time.realtimeSinceStartup - m_AnimCircleStartTime;
				if (num < 0.65f)
				{
					circleImage.fillAmount = Tween.CubeInOut(m_AnimCircleCurrentFillAmount, 0.75f, num, 0.65f);
				}
				else
				{
					m_AnimCircleCurrentFillAmount = 0.75f;
					circleImage.fillAmount = 0.75f;
					FlipCircle(clockwise: false);
					m_AnimCircleStartTime = Time.realtimeSinceStartup;
					m_AnimCircle = 2;
				}
				RectTransform circleRectTransform = m_CircleRectTransform;
				Vector3 localEulerAngles = m_CircleRectTransform.localEulerAngles;
				float x = localEulerAngles.x;
				Vector3 localEulerAngles2 = m_CircleRectTransform.localEulerAngles;
				float y = localEulerAngles2.y;
				Vector3 localEulerAngles3 = m_CircleRectTransform.localEulerAngles;
				circleRectTransform.localEulerAngles = new Vector3(x, y, localEulerAngles3.z - Time.unscaledDeltaTime * 200f);
			}
			else if (m_AnimCircle == 2)
			{
				float num2 = Time.realtimeSinceStartup - m_AnimCircleStartTime;
				if (num2 < 0.65f)
				{
					circleImage.fillAmount = Tween.CubeInOut(m_AnimCircleCurrentFillAmount, 0.1f, num2, 0.65f);
				}
				else
				{
					m_AnimCircleCurrentFillAmount = 0.1f;
					circleImage.fillAmount = 0.1f;
					FlipCircle(clockwise: true);
					m_AnimCircleStartTime = Time.realtimeSinceStartup;
					m_AnimCircle = 1;
				}
				RectTransform circleRectTransform2 = m_CircleRectTransform;
				Vector3 localEulerAngles4 = m_CircleRectTransform.localEulerAngles;
				float x2 = localEulerAngles4.x;
				Vector3 localEulerAngles5 = m_CircleRectTransform.localEulerAngles;
				float y2 = localEulerAngles5.y;
				Vector3 localEulerAngles6 = m_CircleRectTransform.localEulerAngles;
				circleRectTransform2.localEulerAngles = new Vector3(x2, y2, localEulerAngles6.z - Time.unscaledDeltaTime * 200f);
			}
			else if (m_AnimCircle == 3)
			{
				float num3 = Time.realtimeSinceStartup - m_AnimCircleStartTime;
				if (num3 < 0.65f)
				{
					circleImage.fillAmount = Tween.CubeInOut(m_AnimCircleCurrentFillAmount, m_CurrentProgress, num3, 0.65f);
					Vector3 localEulerAngles7 = m_CircleRectTransform.localEulerAngles;
					localEulerAngles7.z = Tween.CubeInOut(m_AnimCircleCurrentRotation, 0f, num3, 0.65f);
					m_CircleRectTransform.localEulerAngles = localEulerAngles7;
					return;
				}
				float currentProgress = m_CurrentProgress;
				circleImage.fillAmount = currentProgress;
				m_AnimCircleCurrentFillAmount = currentProgress;
				Vector3 localEulerAngles8 = m_CircleRectTransform.localEulerAngles;
				localEulerAngles8.z = 0f;
				m_CircleRectTransform.localEulerAngles = localEulerAngles8;
				m_AnimCircleStartTime = Time.realtimeSinceStartup;
				m_AnimCircle = 0;
			}
		}

		private void UpdateAnimColor()
		{
			if (m_AnimColor)
			{
				float num = Time.realtimeSinceStartup - m_AnimColorStartTime;
				if (num < 0.65f)
				{
					circleIcon.color = Tween.CubeInOut(m_AnimColorCurrentColor, m_AnimColorTargetColor, num, 0.65f);
					return;
				}
				circleIcon.color = m_AnimColorTargetColor;
				m_AnimColor = false;
			}
		}

		private void UpdateAnimSize()
		{
			if (!m_AnimSize)
			{
				return;
			}
			float num = Time.realtimeSinceStartup - m_AnimSizeStartTime;
			if (num < 0.65f)
			{
				Vector3 localScale = base.scaledRectTransform.localScale;
				localScale.x = Tween.CubeInOut(m_AnimSizeCurrentSize, m_AnimSizeTargetSize, num, 0.65f);
				localScale.y = localScale.x;
				localScale.z = localScale.x;
				base.scaledRectTransform.localScale = localScale;
				return;
			}
			Vector3 localScale2 = base.scaledRectTransform.localScale;
			localScale2.x = m_AnimSizeTargetSize;
			localScale2.y = localScale2.x;
			localScale2.z = localScale2.x;
			base.scaledRectTransform.localScale = localScale2;
			m_AnimSize = false;
			if (m_AnimSizeTargetSize == 0f)
			{
				base.gameObject.SetActive(value: false);
			}
		}

		public override void Show(bool startIndeterminate = true)
		{
			if (!(base.scaledRectTransform == null))
			{
				base.gameObject.SetActive(value: true);
				Vector3 localScale = base.scaledRectTransform.localScale;
				m_AnimSizeCurrentSize = localScale.x;
				m_AnimSizeTargetSize = 1f;
				m_AnimSizeStartTime = Time.realtimeSinceStartup;
				m_AnimSize = true;
				if (!m_IsAnimatingIndeterminate && startIndeterminate)
				{
					StartIndeterminate();
				}
			}
		}

		public override void Hide()
		{
			if (!(base.scaledRectTransform == null))
			{
				Vector3 localScale = base.scaledRectTransform.localScale;
				m_AnimSizeCurrentSize = localScale.x;
				m_AnimSizeTargetSize = 0f;
				m_AnimSizeStartTime = Time.realtimeSinceStartup;
				m_AnimSize = true;
			}
		}

		public override void StartIndeterminate()
		{
			FlipCircle(clockwise: true);
			SetAnimCurrents();
			m_IsAnimatingIndeterminate = true;
			m_AnimCircle = 1;
			Show();
		}

		public override void SetProgress(float progress, bool animated = true)
		{
			if (!(circleImage == null) && !(circleRectTransform == null))
			{
				progress = Mathf.Clamp(progress, 0f, 1f);
				if (!animated)
				{
					FlipCircle(clockwise: true);
					m_CurrentProgress = progress;
					m_IsAnimatingIndeterminate = false;
					circleImage.fillAmount = m_CurrentProgress;
					Vector3 localEulerAngles = m_CircleRectTransform.localEulerAngles;
					localEulerAngles.z = 0f;
					m_CircleRectTransform.localEulerAngles = localEulerAngles;
					m_AnimCircle = 0;
				}
				else
				{
					FlipCircle(clockwise: true);
					SetAnimCurrents();
					m_CurrentProgress = progress;
					m_IsAnimatingIndeterminate = false;
					m_AnimCircle = 3;
				}
			}
		}

		public override void SetColor(Color color)
		{
			m_AnimColorCurrentColor = circleIcon.color;
			m_AnimColorTargetColor = color;
			m_AnimColorStartTime = Time.realtimeSinceStartup;
			m_AnimColor = true;
		}

		private void SetAnimCurrents()
		{
			if (!(circleImage == null))
			{
				Vector3 localEulerAngles = m_CircleRectTransform.localEulerAngles;
				m_AnimCircleCurrentRotation = localEulerAngles.z;
				m_AnimCircleCurrentFillAmount = circleImage.fillAmount;
				m_AnimCircleStartTime = Time.realtimeSinceStartup;
			}
		}

		private void FlipCircle(bool clockwise)
		{
			if (!(circleImage == null))
			{
				if (!circleImage.fillClockwise && clockwise)
				{
					RectTransform circleRectTransform = m_CircleRectTransform;
					Vector3 localEulerAngles = m_CircleRectTransform.localEulerAngles;
					float x = localEulerAngles.x;
					Vector3 localEulerAngles2 = m_CircleRectTransform.localEulerAngles;
					float y = localEulerAngles2.y;
					Vector3 localEulerAngles3 = m_CircleRectTransform.localEulerAngles;
					circleRectTransform.localEulerAngles = new Vector3(x, y, localEulerAngles3.z + 360f * circleImage.fillAmount);
					circleImage.fillClockwise = true;
				}
				else if (circleImage.fillClockwise && !clockwise)
				{
					RectTransform circleRectTransform2 = m_CircleRectTransform;
					Vector3 localEulerAngles4 = m_CircleRectTransform.localEulerAngles;
					float x2 = localEulerAngles4.x;
					Vector3 localEulerAngles5 = m_CircleRectTransform.localEulerAngles;
					float y2 = localEulerAngles5.y;
					Vector3 localEulerAngles6 = m_CircleRectTransform.localEulerAngles;
					circleRectTransform2.localEulerAngles = new Vector3(x2, y2, localEulerAngles6.z - 360f * circleImage.fillAmount);
					circleImage.fillClockwise = false;
				}
			}
		}

		public override float GetMinWidth()
		{
			return 48f;
		}

		public override float GetMinHeight()
		{
			return 48f;
		}
	}
}
